import requests
from topic import Topic


def get_real_time_rank():
    # 获取实时和飙升榜单
    url = 'https://m.weibo.cn/api/container/getIndex?containerid=106003type%3D25%26t%3D3%26disable_hot%3D1%26filter_type%3Drealtimehot&title=%E5%BE%AE%E5%8D%9A%E7%83%AD%E6%90%9C&extparam=pos%3D0_0%26mi_cid%3D100103%26cate%3D10103%26filter_type%3Drealtimehot%26c_type%3D30%26display_time%3D1584279645&luicode=10000011&lfid=231583'
    headers = {
        "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
    }
    res = requests.get(url, headers=headers)
    real_time_list = []
    rise_list = []
    if res.status_code == 200:
        data = res.json()
        if data['ok'] == 1:
            data = data['data']['cards']
            for card in data[0]['card_group']:
                entry = {}
                entry['title'] = card['desc']
                entry['scheme'] = card['scheme']
                real_time_list.append(entry)
            for card in  data[1]['card_group']:
                entry = {}
                entry['title'] = card['desc']
                entry['scheme'] = card['scheme']
                rise_list.append(entry)

    return real_time_list, rise_list


def get_assigned_rank(day, hour):
    return 1


# 返回指定日期热搜榜单 精确到小时

def get_topic_real_time():
    url = 'https://m.weibo.cn/api/container/getIndex?containerid=106003type%3D25%26t%3D3%26disable_hot%3D1%26filter_type%3Dtopicscene&title=%E8%AF%9D%E9%A2%98%E6%A6%9C&extparam=band_list_gray%3D1&luicode=10000011&lfid=106003type%3D25%26t%3D3%26disable_hot%3D1%26filter_type%3Drealtimehot'
    headers = {
        "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
    }
    response = requests.get(url, headers)
    topic_rank = []
    if response.status_code == 200:
        topic_datas = response.json()
        if topic_datas['ok'] == 1:
            cards = ['data']['cards']
            for card in cards:
                card_group = card['card_group']
                topic_rank.append(Topic(card_group[0]))
    return topic_rank
